package java_thinking.unit_14._14_3;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @Description : 修改RegisteredFactories 使其不要使用显式的工厂，而是将类对象存储到List中，并使用newInstance()来建对象
 * @author: cww
 * @DateTime: 2019-03-30 11:07
 */

public class Part2 {
    @Override
    public String toString() {
        return getClass().getSimpleName();
    }
    static List<Class<? extends Part2>> factories = Arrays.asList(FuelFilter2.class,AirFilter2.class
            ,CabinAirFilter2.class,OilFilter2.class,FanBelt2.class
            ,GeneratorBelt2.class,PowerSteeringBelt2.class);

    private static Random random = new Random(47);
    public static Part2 createRandom(){
        int n = random.nextInt(factories.size());
        try {
            return factories.get(n).newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

class RegisteredFactories2 {
    public static void main(String[] args) {
        for (int i = 0; i<10;i++){
            System.out.println(Part2.createRandom());
        }
    }
}
class Filter2 extends Part2 {}

class FuelFilter2 extends Filter2 {}

class AirFilter2 extends Filter2 {}

class CabinAirFilter2 extends Filter2 {}

class OilFilter2 extends Filter2 {}

class Belt2 extends Part2 {}

class FanBelt2 extends Belt2 {}

class GeneratorBelt2 extends Belt2 {}

class PowerSteeringBelt2 extends Belt2 {}